package com.unohacha.qqj.fzdr.main;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.bean.SupplyDetailBean;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.imp.XUtil;
import com.unohacha.qqj.fzdr.util.GlideImageLoader;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.mylibrary.base.BaseActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class NeedDetailActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.img_share)
    ImageView imgShare;
    @BindView(R.id.RelativeLayout_top)
    RelativeLayout RelativeLayoutTop;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.circleimg)
    RoundedImageView circleimg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_standard)
    TextView tvStandard;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.store_pic)
    ImageView storePic;
    @BindView(R.id.user_name_tv)
    TextView userNameTv;
    @BindView(R.id.user_report_content)
    TextView userReportContent;
    @BindView(R.id.comment_user)
    TextView commentUser;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.collection_iv)
    ImageView collectionIv;
    @BindView(R.id.collection_tv)
    TextView collectionTv;
    @BindView(R.id.collection_ll)
    RelativeLayout collectionLl;
    @BindView(R.id.comment_tv)
    TextView commentTv;
    @BindView(R.id.send_tv)
    TextView sendTv;
    private String supply_cate_id;//栏目ID
    private String address_id;//定位地区ID
    private String id;//ID
    private String token;
    private List<String> bannerlist;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_need_detail;
    }

    @Override
    protected void initView() {
        bannerlist = new ArrayList<>();
        supply_cate_id = this.getIntent().getStringExtra("supply_cate_id");
        address_id = this.getIntent().getStringExtra("address_id");
        id = this.getIntent().getStringExtra("id");
        token = Prefer.getInstance().getToken();
        getMessage();
    }

    private void getMessage() {
        RetrofitManager.getInstance()
                .supplyDetail(id, supply_cate_id, address_id, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Action1<HttpResult<SupplyDetailBean>>() {
                    @Override
                    public void call(HttpResult<SupplyDetailBean> supplyDetailBeanHttpResult) {
                        if (supplyDetailBeanHttpResult.getCode() == 1) {
                            RefreshUi(supplyDetailBeanHttpResult.getData());
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    private void getDetail() {
//        XUtil.
    }

    private void RefreshUi(SupplyDetailBean date) {
        if (null != date.getList_pic() && date.getList_pic().size() > 0) {
            bannerlist.addAll(date.getList_pic());
        }
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置圆形指示器与标题
        banner.setIndicatorGravity(BannerConfig.CENTER);//设置指示器位置
        banner.setImages(bannerlist);
        banner.setImageLoader(new GlideImageLoader());
        banner.start();

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //用mBannerAllList集合
            }
        });
        Glide.with(this).load(date.getMember_thumb()).error(R.drawable.ic_default_photo).into(circleimg);
        tvName.setText(date.getMember_no());
        tvTime.setText(date.getCreate_time() + "发布");
        tvContent.setText(date.getTitle());
        tvType.setText("分类：" + date.getSupply_cate_id());
        tvStandard.setText(date.getSpec());

    }

    @OnClick({R.id.img_back, R.id.img_share, R.id.banner, R.id.tv_more, R.id.tv_phone, R.id.collection_ll, R.id.comment_tv, R.id.send_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_share:
                break;
            case R.id.banner:
                break;
            case R.id.tv_more:
                break;
            case R.id.tv_phone:
                break;
            case R.id.collection_ll:
                break;
            case R.id.comment_tv:
                break;
            case R.id.send_tv:
                break;
        }
    }
}
